package top.infopub.mgr.sys.domain;


import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.beanutils.BeanUtils;

import top.infopub.model.sys.Country;
import top.infopub.model.sys.Province;


public class CountryDomain extends Country {

    private static final long serialVersionUID = -5979943647243498949L;

    private Set<Province> pros = new TreeSet<Province>(new ProComparator());

    public void addProvince(Province pro) {
        boolean addFlag = true;
        for (Province apro : pros) {
            if (apro.getId().equalsIgnoreCase(pro.getId())) {
                addFlag = false;
                break;
            }
        }
        if (addFlag) {
            pros.add(pro);
        }
    }

    public Set<Province> getListPros() {
        Set<Province> _tmp = new TreeSet<Province>(new ProComparator());
        for (Province pro : pros) {
            Province dest = new Province();
            try {
                BeanUtils.copyProperties(dest, pro);
                _tmp.add(dest);
            }
            catch (Exception e) {
                //do nothing
            }
        }
        return _tmp;
    }

    class ProComparator implements Comparator<Province> {
        @Override
        public int compare(Province o1, Province o2) {
            if (o1.getIsort() == null || o2.getIsort() == null) {
                return -1;
            }
            if (o1.getIsort() > o2.getIsort()) {
                return 1;
            }
            else {
                return -1;
            }
        }
    }

}
